package br.com.SGF.bean;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import br.com.SGF.dao.ClienteDAO;
import br.com.SGF.dao.ConectaBancoMVC;
import br.com.SGF.domain.CidadeDomain;
import br.com.SGF.domain.ClienteDomain;
import br.com.SGF.util.JSFUtil;

@ManagedBean(name = "RelatorioMB")
@ViewScoped
public class RelatorioBean {
	Integer id = 0;
	Date dataAlteracao;
	ConectaBancoMVC c = new ConectaBancoMVC();

	private ClienteDomain clienteDomain = new ClienteDomain();

	private List<ClienteDomain> clientes;

	private List<CidadeDomain> listaCidade;
	private List<ClienteDomain> clientesFiltrados;

	public Date getDataAlteracao() {
		return dataAlteracao;
	}

	public void setDataAlteracao(Date dataAlteracao) {
		this.dataAlteracao = dataAlteracao;
	}

	public List<CidadeDomain> getListaCidade() {
		return listaCidade;
	}

	public void setListaCidade(List<CidadeDomain> listaCidade) {
		this.listaCidade = listaCidade;
	}

	public List<ClienteDomain> getClientesFiltrados() {
		return clientesFiltrados;
	}

	public void setClientesFiltrados(List<ClienteDomain> clientesFiltrados) {
		this.clientesFiltrados = clientesFiltrados;
	}

	public List<ClienteDomain> getClientes() {
		return clientes;
	}

	public void setClientes(List<ClienteDomain> clientes) {
		this.clientes = clientes;
	}

	public ClienteDomain getClienteDomain() {
		return clienteDomain;
	}

	public void setClienteDomain(ClienteDomain clienteDomain) {
		this.clienteDomain = clienteDomain;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void gerarRelatorioCliente() {

		try {

			InputStream caminho = FacesContext.getCurrentInstance()
					.getExternalContext()
					.getResourceAsStream("/reports/Template2.jasper");

			// JasperReport report =
			// JasperCompileManager.compileReport(caminho);

			JasperPrint print = JasperFillManager.fillReport(caminho, null,
					c.obterConexao());

			byte[] bytes = JasperExportManager.exportReportToPdf(print);
			if (bytes != null) {
				HttpServletResponse response = JSFUtil.getResponse();
				response.setContentType("application/pdf");
				response.setHeader("Content-disposition",
						"attachment;filename=Relatorio Cliente.pdf");
				response.setContentLength(bytes.length);
				response.getOutputStream().write(bytes, 0, bytes.length);
				response.getOutputStream().flush();
				response.flushBuffer();
				JSFUtil.responseComplete();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void gerarRelatorioCidade() {

		try {

			InputStream caminho = FacesContext.getCurrentInstance()
					.getExternalContext()
					.getResourceAsStream("/reports/Template2.jasper");

			// JasperReport report =
			// JasperCompileManager.compileReport(caminho);

			JasperPrint print = JasperFillManager.fillReport(caminho, null,
					c.obterConexao());

			byte[] bytes = JasperExportManager.exportReportToPdf(print);
			if (bytes != null) {
				HttpServletResponse response = JSFUtil.getResponse();
				response.setContentType("application/pdf");
				response.setHeader("Content-disposition",
						"attachment;filename=Relatorio Cidade.pdf");
				response.setContentLength(bytes.length);
				response.getOutputStream().write(bytes, 0, bytes.length);
				response.getOutputStream().flush();
				response.flushBuffer();
				JSFUtil.responseComplete();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void relatorioParametro() {
		HashMap<String, Object> parametros = new HashMap<String, Object>();

		try {

			System.out.println(dataAlteracao);
			if (id == 0) {
				parametros.put("cidade", "");
				parametros.put("data_alteracao", dataAlteracao);
				System.out.println(dataAlteracao);
			} else {
				parametros.put("cidade", id);
				parametros.put("data_alteracao", dataAlteracao);
				System.out.println(dataAlteracao);
			}

			InputStream relatorio = FacesContext.getCurrentInstance()
					.getExternalContext()
					.getResourceAsStream("/reports/cliente.jasper");

			JasperPrint print = JasperFillManager.fillReport(relatorio,
					parametros, c.obterConexao());

			byte[] bytes = JasperExportManager.exportReportToPdf(print);
			if (bytes != null) {

				HttpServletResponse response = JSFUtil.getResponse();
				response.setContentType("application/pdf");
				response.setHeader("Content-disposition",
						"attachment;filename=RelatorioParametro.pdf");
				response.setContentLength(bytes.length);
				response.getOutputStream().write(bytes, 0, bytes.length);
				response.getOutputStream().flush();
				response.flushBuffer();
				JSFUtil.responseComplete();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void carregarClientes() {
		try {
			ClienteDAO cliDao = new ClienteDAO();
			clientes = cliDao.listarCliente();
		} catch (RuntimeException ex) {
			ex.printStackTrace();
			JSFUtil.adicionarMensagemErro(ex.getMessage());

		}
	}

	@Override
	public String toString() {
		return "RelatorioBean [id=" + id + ", dataAlteracao=" + dataAlteracao
				+ ", c=" + c + ", clienteDomain=" + clienteDomain
				+ ", clientes=" + clientes + ", listaCidade=" + listaCidade
				+ ", clientesFiltrados=" + clientesFiltrados + "]";
	}

	public void gerarRelatorioCargoUsuario() {
		HashMap<String, Object> parametros = new HashMap<String, Object>();

		try {

			parametros.put("$P{SUBCARGO}", "SUBREL_CARGUSUARIO.jasper");

			InputStream relatorio = FacesContext.getCurrentInstance()
					.getExternalContext()
					.getResourceAsStream("/reports/Cargo_Cliente.jasper");

			JasperPrint print = JasperFillManager.fillReport(relatorio,
					parametros, c.obterConexao());

			byte[] bytes = JasperExportManager.exportReportToPdf(print);
			if (bytes != null) {

				HttpServletResponse response = JSFUtil.getResponse();
				response.setContentType("application/pdf");
				response.setHeader("Content-disposition",
						"attachment;filename=RelatorioParametro.pdf");
				response.setContentLength(bytes.length);
				response.getOutputStream().write(bytes, 0, bytes.length);
				response.getOutputStream().flush();
				response.flushBuffer();
				JSFUtil.responseComplete();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
